package test.enba.payment.controller.alipay;

import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson2.JSON;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.v3.ApiException;
import com.enba.boot.payment.alipay.AliPayHelper;
import com.enba.boot.payment.base.PayProductBase;
import com.enba.boot.payment.base.RefundProductBase;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ali-payment")
@Slf4j
public class AliPaymentController {

  @Autowired(required = false)
  private AliPayHelper aliPayHelper;

  // 业务系统订单号
  private final String outTradeNo = IdUtil.fastSimpleUUID();

  // 业务系统退款单号
  private final String outRefundNo = IdUtil.fastSimpleUUID();

  /**
   * 当面付-生成交易付款码
   *
   * @param httpResponse
   * @throws Exception
   */
  @GetMapping("/faceToFace")
  public void faceToFace(HttpServletResponse httpResponse) throws Exception {
    PayProductBase payProductBase = new PayProductBase();
    payProductBase.setNotifyUrl("");
    payProductBase.setOutTradeNo(outTradeNo);
    payProductBase.setSubject("测试商品");
    payProductBase.setTotalAmount("10");

    AlipayTradePrecreateResponse response = aliPayHelper.faceToFace(payProductBase);

    log.info("response###{}", JSON.toJSONString(response));

    // 3. 处理响应或异常
    if (ResponseChecker.success(response)) {
      QrCodeUtil.generate(response.getQrCode(), 256, 256, "", httpResponse.getOutputStream());
    }
  }

  /**
   * 交易退款
   *
   * @return
   * @throws Exception
   */
  @GetMapping("/refund")
  public Object refund() throws Exception {
    RefundProductBase refundProductBase = new RefundProductBase();
    refundProductBase.setOutRefundNo(outRefundNo);
    refundProductBase.setOutTradeNo(outTradeNo);
    refundProductBase.setRefundAmount("10");
    refundProductBase.setRefundReason("测试退款");

    return aliPayHelper.refund(refundProductBase);
  }

  /**
   * 订单查询
   *
   * @return
   * @throws Exception
   */
  @GetMapping("/query")
  public Object query() throws Exception {
    // 业务系统订单号
    return aliPayHelper.query(outTradeNo);
  }

  /**
   * 退款查询
   *
   * @return
   * @throws Exception
   */
  @GetMapping("/refundQuery")
  public Object refundQuery() throws Exception {
    RefundProductBase refundProductBase = new RefundProductBase();
    // 业务系统订单号
    refundProductBase.setOutTradeNo(outTradeNo);
    // 业务退款单号
    refundProductBase.setOutRefundNo(outRefundNo);

    return aliPayHelper.refundQuery(refundProductBase);
  }

  /**
   * 支付宝回调 参考官方文档（https://opendocs.alipay.com/solution/0df1nk） 注意：post方式 返回值为success
   *
   * @param request r
   * @return r
   * @throws ApiException a
   */
  @PostMapping("/callback")
  public String callback(HttpServletRequest request) throws ApiException {

    return aliPayHelper.notifyCallback(
        request,
        (param) -> {
          // 根据回调结果自定义业务实现，这里简单打印
          log.info("支付宝回调：{}", com.alibaba.fastjson.JSON.toJSONString(param));

          return "success";
        });
  }
}
